from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from .models import Area
from . import serializers
# Create your views here.


class AreasViewset(CacheResponseMixin,ReadOnlyModelViewSet):
    """省市区三级联动数据"""

    # 禁用分页
    pagination_class = None

    # 指定查询集
    # queryset = Area.objects.all()
    def get_queryset(self):
        # 根据不同的行为，指定不同的查询集
        if self.action == 'list':
            # 如果是list行为，表示返回订顶级数据
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    # 指定序列化器
    # serializer_class = serializers.AreaSerializer
    def get_serializer_class(self):
        # 根据不同的行为，指定不同的序列化器
        if self.action == 'list':
            return serializers.AreaSerializer
        else:
            return serializers.SubAreaSerializer